package jsp;

import exception.EncodingErrorException;
import util.CookieUtil;
import util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

/**
 * @author MissChen
 * @date 2024年05月14日 15:01
 */

/**
 * 用户访问Servlet，先对当前Cookie中的数据进行检查
 * 判断当前Cookie中是否包含username和password
 * 如果存在
 *      则判断当前的username和password是否正确
 *          如果正确
 *              则直接进入到主页main.jsp
 *          如果不正确
 *              移除当前Cookie中保存的username和password
 *              则进入到登录页面login.jsp，需要用户进行登录
 * 如果不存在
 *      则进入到登录页面login.jsp，需要用户进行登录
 */

@WebServlet("/showMain")
public class ShowMainServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //取出Cookie中的值
        try {
            String username = CookieUtil.findByName("username", request);
            String password = CookieUtil.findByName("password", request);
            System.out.println(username);
            System.out.println(password);
            if (StringUtil.isNull(username) || StringUtil.isNull(password)) {
                request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
                return;
            }
            //如果Cookie中的数据与数据库的数据一致，则表示可以直接跳转到主页，无需登录
            Connection conn = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            String url = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8";
            try {
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection(url, "root", "root");
                String sql=new StringBuffer()
                        .append(" select id,username,password ")
                        .append(" from t_user ")
                        .append(" where username=? ")
                        .append(" and password=? ")
                        .toString();
                ps=conn.prepareStatement(sql);
                ps.setString(1,username);
                ps.setString(2,password);
                rs=ps.executeQuery();
                if(rs.next()){
                    //登录成功之后直接跳转到主页
                    request.getRequestDispatcher("/jsp/main.jsp").forward(request, response);
                    return;
                }
                // 如果Cookie中的数据与数据库的数据不一致，则说明当前保存的Cookie中的数据需要被移除
                CookieUtil.deleteCookie("username",response);
                CookieUtil.deleteCookie("password",response);

                //登录失败就需要进入login.jsp进行登录
                request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);

            } catch (ClassNotFoundException e) {
                e.printStackTrace();

            } catch (SQLException e) {
                e.printStackTrace();

            } finally {
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                if (ps != null) {
                    try {
                        ps.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }


        } catch (EncodingErrorException e) {
            e.printStackTrace();
        }
    }
}
